package Chuong06;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class BT4 extends JFrame implements Runnable{
	Thread t;
	JPanel canvas = new JPanel();
	JButton
		b1 = new JButton("Start"), 
		b2 = new JButton("Suspend"),
		b3 = new JButton("Resume"),
		b4 = new JButton("Close");
	
	public BT4(){
		super("Thread Demo 2");
		add(canvas, BorderLayout.CENTER);
		canvas.setBackground(Color.WHITE);
		
		JPanel panelButton = new JPanel();
		panelButton.setLayout(new FlowLayout());
		panelButton.add(b1);
		panelButton.add(b2);
		panelButton.add(b3);
		panelButton.add(b4);
		b1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t.start();
			}
		}
		);
		b2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t.suspend();
			}
		}
		);
		b3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t.resume();
			}
		}
		);
		b4.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t.stop();
				System.exit(0);
			}
		}
		);
		add(panelButton, BorderLayout.SOUTH);
		setSize(new Dimension(400,600));
		t = new Thread(this);
	}
	
	public void run(){
		int width = getSize().width;
		int height = getSize().height;
		while(true){
			Graphics g = canvas.getGraphics();
			Color c = new Color((int)(255.0*Math.random()),
								(int)(255.0*Math.random()),
								(int)(255.0*Math.random()));
			g.setColor(c);
			
			for(int i=0; i<width/2;i++) {
				g.drawLine(i,0,i,(int)height);
				g.drawLine(width-i,0,width-i,height);
				try{
					Thread.sleep(10);
				}
				catch(InterruptedException e){}
			}
			c = new Color((int)(255.0*Math.random()),
					(int)(255.0*Math.random()),
					(int)(255.0*Math.random()));
			g.setColor(c);
			for(int i = width/2; i>0 ;i--) {
				g.drawLine(i,height,i,0);
				g.drawLine(width - i,height, width - i,0);
				try{
					Thread.sleep(10);
				}
				catch(InterruptedException e){}
			}
		}
	}
	
	public static void main(String[] args){
		BT4 frame = new BT4();
		frame.setVisible(true);
	}
}
